
/**
 * name:        个人中心--售后服务步骤四
 * author:      wuwq & lyj
 * Time:        18/01/26
 * Copyright:   chinadangmai.com
 */

$(function(){
    // 个人中心首页 

    if($.cookie('user')!=''&&$.cookie('user')!=null){
        var token='' 
        isLogin();    
        getToken();     // 获取/验证token 
        if( token!='' && token!=null){
            var refundId =GetQueryString("refundId"); 
            var goodsId =GetQueryString("goodsId"); 
            getRefundOrderDetail(token,URL,refundId,goodsId);
        }
    }else{
        window.location.href='/src/login/login.html?type=2'
    }

    // 导航栏显示已登录
    function isLogin(){
        $('.yes_or_no').remove();
        $('.mod_login').addClass('mod_login2')
        var nav=''
        nav+='<span class="hello" style="margin-left:50px;">您好，</span><span class="nike_name"></span>'
        // nav+='<span class="avater"><img src="../images/base/member_msg_pic.png" alt=""></span>'
        nav+='<i class="icon_down"></i>'
        nav+='<ul class="logined">'
        nav+='<li class="member_center"><a href="../member/member_index.html"><i></i><span>会员中心</span></a></li>'
        nav+='<li class="login_out"><a href="javascript:;"><i></i><span>退出登录</span></a></li>'
        nav+='</ul>'
        $('.mod_login').append(nav);
    }

    // 获取/验证token
    function getToken(){
        strs=$.cookie('user').split(",");
        for(var i in strs){ 
            $('.nike_name').html(strs[1]) 
            token=strs[6];
            name=strs[1];
        }
    }
    // 退出登录
    $('.login_out').click(function(){
        $.cookie('user', null, { path: '/' });
            $.ajax({
                type:'post',
                url:new_url+'shop/login/logOut',
                dataType:'json',
                data:'',
                success:function(data){
                    tc(data.message)
                }  
            })
            setTimeout(function(){
                window.location.href='/index.html'
            },1100);
    });

    

});


function GetQueryString(name){
    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if(r!=null)return  decodeURI(r[2]); return null;
}

function getRefundOrderDetail(token,URL,refundId,goodsId){
    $.ajax({
        type:'post',
        url:new_url+'shop/member_return/getRefundOrderDetail',
        dataType:'json',
        data:{token:token,refundId:refundId,goodsId:goodsId},
        success:function(e){ 
            if(e.data.orderInfo.spec=='' || e.data.orderInfo.spec==null){
                e.data.orderInfo.spec='默认';
            }
            var serviceGoodsTpl = $("#serviceGoodsTpl").html();
            var time = e.data.orderInfo.createTime;
            var refundState = e.data.orderInfo.refundState;
            function getLocalTime(nS) {     
                return new Date(parseInt(nS) * 1000).toLocaleString().replace(/年|月/g, " ").replace(/日/g, " ");  
            }
            // 退款成功时间
            $("#time").text(getLocalTime(time));
            // 退款金额
            $("#price").text('￥'+e.data.orderInfo.refundAmount);
            // 售后状态 1.待审核 2.审核不通过 3.审核通过 4.等待买家发货 5.买家已发货 6.卖家未收到货 7.已完成 8.退款关闭
            if(refundState == 1){
                refundState = '待审核';
            }
            if(refundState == 2){
                refundState = '审核不通过';
                $('.service_step_tit').remove();
            }
            if(refundState == 3){
                refundState = '审核通过';
            }
            if(refundState == 4){
                refundState = '等待买家发货';
            }
            if(refundState == 5){
                refundState = '买家已发货';
            }
            if(refundState == 6){
                refundState = '卖家未收到货';
            }
            if(refundState == 7){
                refundState = '卖家已收货';
            }
			if(refundState == 8){
                refundState = '退款已完成';
            }
			if(refundState == 9){
                refundState = '退款关闭';
            }
            // 商品信息
            var serviceGoodsCont=template(
                serviceGoodsTpl, {
                    serviceGoodsList: [{
                        createTime:getLocalTime(time),
                        goodsImg:e.data.orderInfo.image,
                        goodsName:e.data.orderInfo.name,
                        goodsSpec:e.data.orderInfo.spec,
                        orderNo:e.data.orderInfo.orderNo,
                        payPrice:e.data.orderInfo.payPrice,
                        refundAmount:e.data.orderInfo.refundAmount,
                        refundId:e.data.orderInfo.refundId,
                        refundNo:e.data.orderInfo.refundNo,
                        refundNum:e.data.orderInfo.refundNum,
                        refundState:refundState,
                    }]
                }
            );
            $('.service_goods').append(serviceGoodsCont);
            

            // 售后处理日志
            if(e.data.handleLog.length != 0){
                var handleLogTpl = $("#handleLogTpl").html();
                var handleLogCont=template(
                    handleLogTpl, {
                        handleLogList: [{
                            logisticsName:e.data.handleLog.name,
                            logisticsNo:e.data.handleLog.logisticsNo,
                        }]
                    }
                );
                $('.service_wuliu').append(handleLogCont);
            }else{
                $(".service_record_cont").append('<div style="width:100%;height:100px;line-height:100px;text-align:center;">暂无售后处理消息</div>')
            }
            
        }
    });
}